#include <map>
#include <fstream>
#include <vector>

#ifndef String
#include "..\..\Paril\Utils\String.h"
#endif

#ifndef byte
typedef unsigned char byte;
#endif

#ifndef BlockAndData
#include "BlockAndData.h"
#endif

class BlockNames
{
	std::map<BlockAndData, std::string> _blockNames;

public:
	BlockNames()
	{
		std::fstream file("blocks.txt", std::ios_base::in);

		while (!file.eof())
		{
			std::string line;

			while (char c = file.get())
			{
				if (c == '\r')
					continue;
				else if (c == '\n' || file.eof())
					break;

				line += c;
			}

			auto data = String::Split<std::vector<std::string>>(line, ":", false);
			auto vals = String::Split<std::vector<std::string>>(data[0], ",", false);

			if (vals.size() == 1)
				Add(std::atoi(String::Trim(vals[0]).c_str()), String::Trim(data[1]));
			else
				Add(std::atoi(String::Trim(vals[0]).c_str()), std::atoi(String::Trim(vals[1]).c_str()), String::Trim(data[1]));
		}
	}

	void Add(const int &id, const int &data, const std::string &name)
	{
		_blockNames[BlockAndData(id, data)] = name;
	}

	void Add(const int &id, const std::string &name)
	{
		Add(id, -1, name);
	}

	const std::string &Get(const int &id, const int &data)
	{
		auto it = _blockNames.find(BlockAndData(id, -1));
		
		if (it != _blockNames.end())
			return (*it).second;

		it = _blockNames.find(BlockAndData(id, data));

		if (it != _blockNames.end())
			return (*it).second;

		static const std::string &unk = "Unknown";
		return unk;
	}

	const std::string &Get(const int &id)
	{
		return Get(id, -1);
	}
};